package com.tuling.designpattern.distinct;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 林瑞涛
 * @desc
 * @date 2021年08月28日 14:24
 */
public class Test {

    public static void main(String[] args) {
        List<RuleItem> ruleItemList = new ArrayList<>();

        RuleItem ruleItem1 = new RuleItem(1, 3);
        RuleItem ruleItem2 = new RuleItem(3, 5);
        RuleItem ruleItem3 = new RuleItem(5, 8);


        ruleItemList.add(ruleItem1);
        ruleItemList.add(ruleItem3);
        ruleItemList.add(ruleItem2);
        //原始数组
        System.out.println(ruleItemList);

        //首先按照最小值从小排序
        ruleItemList = ruleItemList.stream().sorted(Comparator.comparing(RuleItem::getMin)).collect(Collectors.toList());
        //按照最小值升序排序
        System.out.println(ruleItemList);
        //校验区间是否交叉
        for (int i = 0; i < ruleItemList.size()-1; i++) {
            RuleItem current = ruleItemList.get(i);
            RuleItem next = ruleItemList.get(i+1);
            if(current.getMin() >= current.getMax()){
                System.out.println("同个区间不允许最小值大于或等于最大值");
                return;
            }
            if(current.getMax() > next.getMin()){
                System.out.println("区间不允许交叉");
                return;
            }
        }
        System.out.println("区间列表没问题");
    }
}
